package com.talent.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.talent.web.annotation.SubmitLimit;
import com.talent.web.annotation.TalentLog;
import com.talent.web.enums.ActionType;
import com.talent.web.model.TalentResult;
import com.talent.web.model.dto.AllocationPermissionDto;
import com.talent.web.model.dto.SysRoleDto;
import com.talent.web.model.entity.SysRole;
import com.talent.web.model.query.RoleQuery;
import com.talent.web.model.vo.OptionVo;
import com.talent.web.service.ISysRoleService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName：RoleController
 * @Author: hszhou
 * @Date: 2024/10/21 13:35
 * @Description: 角色控制器
 */
@RestController
@RequestMapping("role")
public class RoleController {


    @Autowired
    private ISysRoleService roleService;


    @GetMapping("option")
    public TalentResult<List<OptionVo>> page(){
        return TalentResult.success(roleService.option());
    }

    @GetMapping("page")
    public TalentResult<IPage<SysRole>> page(@RequestParam(defaultValue = "1") int current,
                                             @RequestParam(defaultValue = "10") int pageSize, RoleQuery query){
        IPage<SysRole> page = roleService.page(current, pageSize, query);
        return TalentResult.success(page);
    }

    @PostMapping
    @TalentLog(module="角色",action = ActionType.INSERT)
    @SubmitLimit(headers = {"Authorization"})
    public TalentResult<Null> addSysRole(@RequestBody SysRoleDto dto){
        roleService.saveRole(dto);
        return TalentResult.success(null);
    }

    @DeleteMapping("{id}")
    @TalentLog(module="角色",action = ActionType.DELETE)
    public TalentResult<Null> deleteById(@PathVariable("id")Long id){
        roleService.deleteRoleById(id);
        return TalentResult.success(null);
    }

    @PutMapping
    @TalentLog(module="角色",action = ActionType.UPDATE)
    public TalentResult<Null> updateRole(@RequestBody SysRoleDto dto) {

        roleService.updateRole(dto);
        return TalentResult.success(null);
    }

    @GetMapping("/permission/{roleId}")
    public TalentResult<List<String>> permission(@PathVariable("roleId")Long roleId){
        List<String> hasPermissions = roleService.hasPermission(roleId);
        return TalentResult.success(hasPermissions);
    }

    @PostMapping("/allocationPermission")
    @TalentLog(module="角色",action = ActionType.ASSIGN)
    public TalentResult<Void> allocationPermission(@RequestBody AllocationPermissionDto dto){
         roleService.allocationPermission(dto);
        return TalentResult.success();
    }


}
